package com.example.payer

import android.graphics.Color
import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.payer.databinding.ActivityPayerDetailBinding
import com.example.payer.etity.Payer
import com.example.payer.vm.PayerViewModel
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/payer/PayerDetailActivity")
class PayerDetailActivity : BaseMvvmActivity<ActivityPayerDetailBinding>() {
    private val payerDetailViewModel:PayerViewModel by viewModels()
    @Autowired
    lateinit var payer: Payer
    override fun initView() {
        ARouter.getInstance().inject(this)
        payerDetailViewModel.getPayerDetail(payer.id)
        mDataBinding.tvName.text = "户主: ${payer.payerName}"
        mDataBinding.tvAddress.text = "天津八维培训学校"
        mDataBinding.tvPhone.text = "联系电话: ${payer.contactNumber}"
        mDataBinding.tvMoney.text = "账单金额: ￥${payer.actualAmount}"
        payerDetailViewModel.mPayerDetailSuccess.observe(this){
            //根据状态判断按钮变色
            if(it.paymentStatus == "已缴费"){
                mDataBinding.btnPay.setBackgroundColor(Color.GRAY)
                mDataBinding.bar2.visibility = View.GONE
            }else{
                mDataBinding.btnPay.setBackgroundColor(Color.BLUE)
                mDataBinding.bar2.visibility = View.VISIBLE
            }
            //全选
            mDataBinding.cbAll.setOnClickListener {
                if(mDataBinding.cbAll.isChecked == true){
                    payerDetailViewModel.mPayerDetailSuccess.observe(this){
                        mDataBinding.checkMoney.text = "已选账单金额: ￥${it.actualAmount}"
                    }
                }else{
                    mDataBinding.checkMoney.text = null
                }
            }
        }
        //点击更多弹出支付方式
        mDataBinding.more.setOnClickListener {
            val list = mutableListOf("支付宝二维码","微信","银行卡")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("请选择支付方式")
            optionPicker.show()
        }
        //点击新增跳转临时收费
        mDataBinding.tvAdd.setOnClickListener {
            payerDetailViewModel.mPayerDetailSuccess.observe(this){
                ARouter.getInstance().build("/payer/AddPayerActivity").withInt("payerId",it.payerId).navigation()
            }
        }
        //点击收款跳转到缴费页面
        mDataBinding.btnPay.setOnClickListener {
            if(mDataBinding.cbAll.isChecked){
                payerDetailViewModel.mPayerDetailSuccess.observe(this){
                    ARouter.getInstance().build("/payer/PayerUpDateActivity").withSerializable("payer",it).navigation()
                }
            }
        }
        //返回
        mDataBinding.back.setOnClickListener {
            finish()
        }
        //请求失败
        payerDetailViewModel.mError.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_payer_detail
}